package DaoImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import Dao.UserDao;
import Entity.Activity;
import Entity.Stlist;
import Entity.User;
import Util.HibernateUtil;

public class UserDaoImpl implements UserDao {
//登陆
	@Override
	public User login(User user) {
		Transaction ts = null;
		String hql = "";
		Session session=null;
		try {
			 session= HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			hql = "from User where studentId=? and stupassword=?";
			Query query = session.createQuery(hql);
			query.setParameter(0, user.getStudentId());
			query.setParameter(1, user.getStupassword());
			List listUser = query.list();
			ts.commit();
			if (listUser.size() > 0) {
				System.out.println("Yes");
				return (User)listUser.get(0);
			} 
			else {
				System.out.println("No");
				return null;
			}
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();
			return null;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
			
		}
	}
	//查看信息
	@Override
	public User findInformation(String id) {
		Transaction ts = null;
		String hql = "";
		Session session=null;
		try {
		    session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			hql = "from User where studentId=?";
			Query query = session.createQuery(hql);
			query.setParameter(0, id);
			List listUser = query.list();
			ts.commit();
			if (listUser.size() > 0) {
				User user=(User) listUser.get(0);
				return user;
			} 
			else {
				System.out.println("No");
				return null;
			}	
		} 
		catch (Exception ex) {
			ex.printStackTrace();
			ts.rollback();;
			return null;
		}
		finally {
			if (ts != null) {
				ts = null;
			}
			
		}
	}


	//更新信息
	@Override
	public boolean updateInformation(User user) {
		Transaction ts=null;
		Session session=null;
		try {
			session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.update(user);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
	}
	//加入社团
	@Override
	public boolean joinSt(String userid, String stid) {
		// TODO Auto-generated method stub
		Transaction ts=null;
		Session session=null;
		try{
			 session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			Stlist st=(Stlist) session.get(Stlist.class,stid);
			User user=(User) session.get(User.class, userid);
			st.getSt_uslist().add(user);
			session.save(st);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
		
	}
	//退出社团
	@Override
	public boolean outSt(String userid, String stid) {
		Transaction ts=null;
		Session session=null;
		try{
			session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			Stlist st=(Stlist) session.get(Stlist.class,stid);
			User user=(User) session.get(User.class, userid);
			user.getUs_stlist().remove(st);
			session.update(user);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
	}
	//添加学生
	@Override
	public boolean addUser(User user) {
		Transaction ts=null;
		Session session=null;
		try{
			session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			session.save(user);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
	}
	//参加活动
	@Override
	public boolean addActivity(String userid, String activityid) {
		// TODO Auto-generated method stub
		Transaction ts=null;
		Session session=null;
		try{
			 session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			Activity activity=(Activity) session.get(Activity.class,activityid);
			User user=(User) session.get(User.class, userid);
			activity.getAc_uslist().add(user);
			session.save(activity);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
	}
	//退出活动
	@Override
	public boolean outActivity(String userid, String activityid) {
		Transaction ts=null;
		Session session=null;
		try{
			 session=HibernateUtil.getSessionFactory().getCurrentSession();
			ts=session.beginTransaction();
			Activity activity=(Activity) session.get(Activity.class,activityid);
			User user=(User) session.get(User.class, userid);
			activity.getAc_uslist().remove(user);
			session.save(activity);
			ts.commit();
			return true;
		}catch(Exception e)
		{
			ts.rollback();
			e.printStackTrace();
			return false;
		}finally{
			if(ts!=null)
			{
				ts=null;
			}
			
		}
	}

}
